<?php

namespace app\api\model;

use app\common\model\User;
use app\shopapi\model\Shop;
use app\shopapi\model\ShopAdmin;
use app\shopapi\model\ShopFans;
use app\shopapi\model\ShopUser;
use think\Model;

class Stat
{

    //地推的用户详情统计
    public function groundPromotionStat($user_id)
    {
        $user_info = \app\common\model\User::where(['id'=>$user_id])
            ->field('id,nickname,avatar,mobile,level,parent_id,createtime')->find();

        $parent_info = \app\common\model\User::where(['id'=>$user_info->parent_id])
            ->field('id,nickname,mobile')->find();

        $v3_info = $this->getV3User($user_id);

        $user_model = new \app\common\model\User();

        $total_recommend_count = $user_model->getFirstTeamCount($user_id,0);

        $today_recommend_count = $user_model->getFirstTeamCount($user_id,1);

        $yesterday_recommend_count = $user_model->getFirstTeamCount($user_id,2);

        $month_recommend_count = $user_model->getFirstTeamCount($user_id,3);

        $recommend_shop_count = $this->getShopCount($user_id);

        $shop_turnover = $this->getShopTurnover($user_id);

        $shop_fans_count = $this->getShopFansCount($user_id);

        $user_consume_log_model = new UserConsumeLog();

        $total_commission = $user_consume_log_model->forecastCommission($user_id,0);

        $today_commission = $user_consume_log_model->forecastCommission($user_id,1);

        $yesterday_commission = $user_consume_log_model->forecastCommission($user_id,2);

        $month_commission = $user_consume_log_model->forecastCommission($user_id,3);

        $data = [
            'total_recommend_count'=>$total_recommend_count,//累计直推
            'today_recommend_count'=>$today_recommend_count,//当天直推
            'yesterday_recommend_count'=>$yesterday_recommend_count,//昨日直推
            'month_recommend_count'=>$month_recommend_count,//本月直推
            'recommend_shop_count'=>$recommend_shop_count,//推荐的店铺数
            'shop_turnover'=>$shop_turnover,//推荐的店铺累计90营业额
            'shop_fans_count'=>$shop_fans_count,//店铺粉丝数
            'parent_info'=>$parent_info,//直推人信息
            'user_info'=>$user_info,//用户信息
            'v3_info'=>$v3_info,//v3用户信息
            'total_commission'=>$total_commission,//累计到账
            'today_commission'=>$today_commission,//当天预估
            'yesterday_commission'=>$yesterday_commission,//昨日预估
            'month_commission'=>$month_commission,//本月预估
        ];

        return $data;
    }

    //查最近的v3等级用户
    public function getV3User($id){
        $parent = User::where(['id'=>$id])->field('id,nickname,mobile,level,parent_id')->find();
        if ($parent){
            if ($parent->level==3){
                return $parent;
            } else {
                $this->getV3User($parent->parent_id);
            }
        } else {
            return '';
        }

    }

    //推荐的店铺列表
    public function getShopList($user_id)
    {
        $list = ShopUser::alias('su')
            ->join('shop_admin sa','su.mobile = sa.mobile')
            ->join('shop s','s.shop_id = sa.shop_id')
            ->where(['su.recommender_id'=>$user_id,'sa.is_applicant'=>1])
            ->field('s.shop_id,s.shop_name,su.mobile,su.id')
            ->select();
        return $list;
    }

    //推荐的店铺数
    public function getShopCount($user_id)
    {
        $count = ShopUser::alias('su')
            ->join('shop_admin sa','su.mobile = sa.mobile')
            ->join('shop s','s.shop_id = sa.shop_id')
            ->where(['su.recommender_id'=>$user_id,'sa.is_applicant'=>1,'s.audit_status'=>3])
            ->field('s.shop_id,s.shop_name,su.mobile,su.id')
            ->count();
        return $count;
    }

    //推荐的有效店铺数
    public function getValidShopCount($user_id)
    {
        $count = ShopUser::alias('su')
            ->join('shop_admin sa','su.mobile = sa.mobile')
            ->join('shop s','s.shop_id = sa.shop_id')
            ->where(['su.recommender_id'=>$user_id,'sa.is_applicant'=>1,'s.audit_status'=>3,'s.fans_num'=>['>',500]])
            ->field('s.shop_id,s.shop_name,su.mobile,su.id')
            ->count();
        return $count;
    }

    //团队推荐的有效店铺数
    public function getTeamValidShopCount($user_id)
    {
        $team_id = $this->getChildList($user_id);
        $team_id = array_column($team_id,'id');

        $count = ShopUser::alias('su')
            ->join('shop_admin sa','su.mobile = sa.mobile')
            ->join('shop s','s.shop_id = sa.shop_id')
            ->where(['su.recommender_id'=>['in',$team_id],'sa.is_applicant'=>1,'s.audit_status'=>3,'s.fans_num'=>['>',500]])
            ->field('s.shop_id,s.shop_name,su.mobile,su.id')
            ->count();
        return $count;
    }

    //递归获取用户所有下级
    public function getChildList($user_id)
    {
        //第一次调用时初始化
        static $arr=array();
        //通过邀请码查询当前邀请码下级用户
        $data = User::where(['parent_id'=>$user_id])
            ->field('id')
            ->select();
        //遍历获取信息，调用当前方法直至没有下级用户
        foreach ($data as $key => $value) {
            $arr[] = $value;
            $this->getChildList($value['id']);
        }
        return $arr;
    }

    //推荐的店铺累计90营业额
    public function getShopTurnover($user_id)
    {
        $shop_list = $this->getShopList($user_id);

        $shop_id = array_column($shop_list,'shop_id');
        if (count($shop_id)>0){
            $time = time();

            $start_time = $time-90*86400;

            $consume = UserConsumeLog::where('shop_id','in',$shop_id)
                ->where('createtime','between',[$start_time,$time])
                ->sum('pay_price');

            return $consume;
        } else {
            return 0;
        }


    }

    //推荐店铺粉丝数
    public function getShopFansCount($user_id)
    {
        $shop_list = $this->getShopList($user_id);

        $shop_id = array_column($shop_list,'shop_id');

        $fans = ShopFans::where('shop_id','in',$shop_id)->count();

        return $fans;
    }

}